<?php
/**
 * (actions)
 */
namespace App\Admin\Actions\Products\Material;

use App\Models\Product\ProductSupplierM;
use Encore\Admin\Actions\BatchAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Database\Eloquent\Collection;
class BatchMaterialSupplierSuccess extends BatchAction
{
    public $name = '批量通过';

    public function handle(Collection $collection)
    {
        try{
            $count = 0;
            $date = date('Y-m-d H:i:s');
            $userid = Admin::user()->id;
            foreach ($collection as $model) {
                if($model->audit_status == ProductSupplierM::AUDIT_SUCCESS){
                    continue;
                }
                $model->audit_admin_id = $userid;
                $model->audit_time = $date;
                $model->audit_status = ProductSupplierM::AUDIT_SUCCESS;
                $flag = $model->save();
                $flag && $count++;
            }

            return $this->response()->success("审核通过{$count}条记录！")->refresh();
        }catch (\Exception $e){
            report($e);
            return $this->response()->error('系统异常！');
        }
    }
}
